package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.*;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle;

import java.util.ArrayList;
import java.util.List;

import static com.joshondesign.sdljava.SDLUtil.p;
import static com.joshondesign.sdljava.demo.Demo2.clearScreen;
import static com.joshondesign.sdljava.demo.Demo2.fillRect;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/2/11
 * Time: 1:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class Demo3 implements SDLUtil.InitCallback{

    private List<Particle> particles = new ArrayList<Particle>();

    public static void main(String ... args) {
        SDLUtil.init(new Demo3());
    }

    public void callback() {
        if(SDL.SDL_Init(SDL.SDL_INIT_VIDEO| SDLConstants.SDL_INIT_TIMER) < 0) {
            p("init failed!");
            System.exit(1);
        }

        SDL_VideoInfo info = SDL.SDL_GetVideoInfo();
        SDL_Surface surface = SDL.SDL_SetVideoMode(800,600,0,SDL.SDL_SWSURFACE);
        if (surface == null) { p("couldn't create a surface"); SDL.SDL_Quit();  System.exit(1); }

        clearScreen(surface);
        SDL_Event event = new SDL_Event();
        long prev_tick = SDL.SDL_GetTicks();
        long ideal = 1000/30;
        long delay = ideal;
        while(true) {
            update(surface);
            SDL.SDL_UpdateRect(surface, 0, 0, 800, 600);

            long tick = SDL.SDL_GetTicks();
            long diff = tick-prev_tick;
            prev_tick = tick;
            //p("diff = " + diff);
            long jitt = diff-ideal;
            if(diff > ideal) {
                delay = (ideal-jitt);
            }
            if(diff < ideal) {
                delay = (ideal+jitt);
            }
            if(delay < 0) {
                delay = 0;
            }
            if(delay < 0 || delay > 40) {
                p("delay = " + delay);
                delay = ideal;
            }
            SDL.SDL_Delay(delay);
            if(jitt > 4) {
                p("jitter = " + jitt);
            }
            SDL.SDL_PollEvent(event);
            if(event.getType() == SDL_EventType.SDL_KEYDOWN) {
                String key = SDL.SDL_GetKeyName(event.getKey().getKeysym().getSym());
                if("q".equals(key)) break;
            }

        }
        SDL.SDL_Quit();
        p("quit SDL");

    }

    private void update(SDL_Surface surface) {
        if(particles.size() < 100) {
            particles.add(new Particle(
                    (int)(Math.random()*800)
                    ,650
                    ,2+Math.random()*2
            ));
        }

        //clear old particles
        for(Particle part : particles) {
            fillRect(surface,part.x,(int)part.y,20,20, 0xFFFFFF);
        }

        for(Particle part : particles) {
            part.y -= part.vy;
            if(part.y < -40) part.y = 700;
        }

        for(Particle part : particles) {
            fillRect(surface,part.x,(int)part.y,20,20, 0x000000);
        }
    }

    private class Particle {

        private int x;
        private double y;
        private double vy;

        public Particle(int x, int y, double v) {
            this.x = x;
            this.y = y;
            this.vy = v;
        }
    }
}